package com.fiveGroup.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fiveGroup.annotation.Log;
import com.fiveGroup.pojo.vo.DataVo;
import com.fiveGroup.pojo.vo.OrderInfoVo;
import com.fiveGroup.service.OrderInfoService;
import com.fiveGroup.service.OrderInfoVoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//订单信息管理
@RestController
@RequestMapping("/api/Order")
@Slf4j
@RequiredArgsConstructor
public class BackgroundOrderInfoController {

    private final OrderInfoVoService orderInfoVoService;

    private final OrderInfoService orderInfoService;

    @GetMapping("/getAllOrder")
    public DataVo getOrderInfo(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam(name = "orderSn", required = false) String orderSn,
            @RequestParam(name = "productName", required = false) String productName
    ) {
        // 调用分页 + 模糊查询方法
        IPage<OrderInfoVo> orderPage = orderInfoVoService.selectOrderInfoVoPage(page, pageSize, orderSn, productName);
        log.info(orderSn, productName);
        // 组装返回给前端的分页数据
        Map<String, Object> resultData = new HashMap<>();
        resultData.put("records", orderPage.getRecords());
        resultData.put("total", orderPage.getTotal());
        resultData.put("pages", orderPage.getPages());
        resultData.put("current", orderPage.getCurrent());
        resultData.put("size", orderPage.getSize());
        return DataVo.success(resultData);
    }

    // 单个删除订单
    @DeleteMapping("/deleteOrder/{id}")
    @Log(action = "订单管理", detail = "删除单条订单信息")
    public DataVo deleteOrder(@PathVariable Long id) {
        boolean success = orderInfoService.removeById(id);
        if (success) {
            return DataVo.success("删除成功");
        } else {
            return DataVo.fail("删除失败");
        }
    }

    @PostMapping("/batchDeleteOrders")
    @Log(action = "订单管理", detail = "批量订单信息")
    public DataVo batchDeleteOrders(@RequestBody List<Long> ids) {
        boolean success = orderInfoService.removeByIds(ids);
        if (success) {
            return DataVo.success("批量删除成功");
        } else {
            return DataVo.fail("批量删除失败");
        }
    }


}
